//
//  MyMeetingView.swift
//  CleanArchitecture
//
//  Created by siri on 2024/11/6.
//  Copyright © 2024 Tuan Truong. All rights reserved.
//



import SwiftUI
import Combine
import SwiftUIRefresh
import Factory


struct MyMeetingView: View {
    @State var year:String =  String(Date.getCurrentYearNum()) // 获取当前年份
    @State  var month:String = String(Date.getCurrentMonthNum())// 获取当月月份
 
    @State var showDetails = false   // 是否展示周日历
    @State var showingPopup = false // 显示月日历弹窗

    // 定义触发器
    final class Triggers: ObservableObject {
        var load = PassthroughSubject<Void, Never>()
        var reload = PassthroughSubject<Void, Never>()
        var clickDate = PassthroughSubject<Void, Never>()
        var select = PassthroughSubject<Void, Never>()
    }
    
    @StateObject var viewModel: MyMeetingViewModel
    @StateObject var output: MyMeetingViewModel.Output
    @StateObject var cancelBag: CancelBag
    @StateObject var triggers: Triggers

    init(viewModel: MyMeetingViewModel) {
        let triggers = Triggers()
        let cancelBag = CancelBag()
        
        let input = MyMeetingViewModel.Input(
            loadTrigger: triggers.load.eraseToAnyPublisher(),
            reloadTrigger: triggers.reload.eraseToAnyPublisher(),
            selectTrigger: triggers.select.eraseToAnyPublisher(),
            clickDateTrigger: triggers.clickDate.eraseToAnyPublisher()
        )
        
        let output = viewModel.transform(input, cancelBag: cancelBag)
        
        self._viewModel = StateObject(wrappedValue: viewModel)
        self._output = StateObject(wrappedValue: output)
        self._cancelBag = StateObject(wrappedValue: cancelBag)
        self._triggers = StateObject(wrappedValue: triggers)
    }

    var body: some View {
        ZStack {
            VStack(spacing: 0) {
                // 头部
                HeadView(title:"我的会议", showingPopup: $showingPopup)
                // 日历
                VStack {
                    CalendarView(statusCode: showDetails == true ? 1 :0,selectMonth: $month,selectYear: $year)
                        .environmentObject(output)
                }.background(Color.white)
                
                // 上拉条
                HStack {
                    RoundedRectangle(cornerRadius: 10).fill(Color.g_color_line).frame(width: 50,height:5).padding()
                }.frame(minWidth: 0,   maxWidth: .infinity,minHeight: 20,maxHeight: 20).background(Color.g_color_bg_white).onTapGesture {
                    withAnimation{
                        self.showDetails.toggle()
                    }
                }
                
                // 会议信息卡片
                ScrollView(showsIndicators: false){
                    VStack {
                        StateCard(statusCode: 0,title:"部内定例",date: "09:00-10:00",roomName: "A311")
                        StateCard(statusCode: 1,title:"小程序埋点设计",date: "10:00-11:00",roomName:"A311")
                        StateCard(statusCode: 1,title: "移动端会议室刷新",date: "10:00-11:00",roomName: "A311")
                                  StateCard(statusCode: 2,title: "会议室预约设计", date: "13:00-14:00",roomName: "A311")
                        StateCard(statusCode: 0,title:"部内定例",date: "09:00-10:00",roomName: "A311")
                        StateCard(statusCode: 1,title:"小程序埋点设计",date: "10:00-11:00",roomName:"A311")
                        StateCard(statusCode: 1,title: "移动端会议室刷新",date: "10:00-11:00",roomName: "A311")
                                  StateCard(statusCode: 2,title: "会议室预约设计", date: "13:00-14:00",roomName: "A311")
                    }.offset(y: 30).zIndex(0)
                }
                Spacer()
            } .frame( maxHeight: .infinity)
                .background(Color.g_color_bg_grey).zIndex(0).onAppear{
                    triggers.load.send()
                    print(SettingsManager.shared.getToken())
                }
            
            
            // 显示日历弹窗
            if showingPopup {
                Color.g_color_bg_mask
                    .contentShape(Rectangle())
                    .onTapGesture {
                            withAnimation{
                                showingPopup = false
                            }
                    }.edgesIgnoringSafeArea(.all)
                DateSelect()
            }
     
        }
     }
  
    
}


struct MyMeetingView_Preview: PreviewProvider {
    static var previews: some View {
        MyMeetingView(viewModel: MyMeetingViewModel(navigationController: UINavigationController()))
    }
}


extension Container {
    func myMeetingView(navigationController: UINavigationController) -> Factory<MyMeetingView> {
        Factory(self) {
            MyMeetingView(viewModel: MyMeetingViewModel(navigationController: navigationController))
        }
    }
}


//#Preview {
//    MyMeetingView()
//}
